package com.yh.blog.serviceImpl;

import com.yh.blog.config.PersonConfig;
import com.yh.blog.domain.Blog;
import com.yh.blog.mapper.BlogMapper;
import com.yh.blog.service.BlogService;
import com.yh.blog.util.MarkdownUtils;
import com.yh.blog.util.Pager;
import org.apache.ibatis.javassist.NotFoundException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class BlogServiceImpl  implements BlogService {
//    有报错的波浪线无需管他 有强迫症的可以降低检测错误将error改成warning 或者换Resource注解
    @Resource
    private BlogMapper blogMapper;
    @Autowired
    private PersonConfig personConfig;

    @Override
    public boolean append(Blog blog) {
        return this.blogMapper.insert(blog);
    }

    @Override
    public boolean remove(Integer blogId) {
        return this.blogMapper.delete(blogId);
    }

    @Override
    public boolean modify(Blog blog) {
        blog.setUpdate_time(new Date());
        return this.blogMapper.update(blog);
    }

    @Override
    public Blog detail(Integer blogId) {
        return this.blogMapper.selectById(blogId);
    }

    @Override
    public Blog detailWithMark(Integer blogId) throws NotFoundException {
        Blog blog=this.blogMapper.selectById(blogId);
        if(blog==null){
            throw new NotFoundException("博客不存在！");
        }
//        浏览量+1
        blog.setBlogId(blogId);
        blog.setViews(blog.getViews()+1);
        this.blogMapper.update(blog);
//        为了不该变数据库中content的原内容 我们操作另外一个对象
        Blog b=new Blog();
        BeanUtils.copyProperties(blog, b);
        String content=b.getContent();
        b.setContent(MarkdownUtils.markdownToHtmlExtensions(content));
        return b;
    }

    @Override
    public Pager<Blog> list(Blog condition, Integer page) {
        List<Blog> datas=this.blogMapper.selectByCondition(condition, (page-1)*this.personConfig.getPage_size(),this.personConfig.getPage_size());
        int size=this.blogMapper.sizeByCondition(condition);
        return new Pager<>(datas, size, page, this.personConfig.getPage_size());
    }

    @Override
    public List<Blog> recommendList() {
        return this.blogMapper.selectRecommendBlog();
    }

    @Override
    public int blogTotalNums() {
        return this.blogMapper.getTotalBlog();
    }

    @Override
    public int viewsTotalNums() {
        return this.blogMapper.getTotalViews();
    }

    @Override
    public int commentsTotalNums() {
        return this.blogMapper.getTotalComments();
    }

    @Override
    public int messagesTotalNums() {
        return this.blogMapper.getTotalMessages();
    }

    @Override
    public List<Blog> list(Blog condition) {
        return this.blogMapper.selectByCondition(condition, 0, 0);
    }

    @Override
    public Pager<Blog> searchBlog(String searchText, Integer page) {
        List<Blog> datas=this.blogMapper.searchBlog(searchText, (page-1)*this.personConfig.getPage_size(), this.personConfig.getPage_size());
        int size=datas.size();
        return new Pager<>(datas, size, page, this.personConfig.getPage_size());
    }
}
